﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO.Ports;

namespace WpfApp1
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        private SerialPort serialPort;
        public MainWindow()
        {
            InitializeComponent();
            serialPort = new SerialPort();
            InitFigure();
            InitSerialPort();
        }
        
        private void  InitFigure()//初始化窗口
        {
            String[] serialname = SerialPort.GetPortNames();
            combobox_SerialPortName.ItemsSource = serialname;
            combobox_SerialPortName.SelectedIndex = serialname.Length - 1;

            combobox_BaundRate.ItemsSource = new String[]{"9600","115200"};
            combobox_BaundRate.SelectedIndex = 0;

            combobox_CheckBit.ItemsSource = new String[] { "None","Odd","Even","Mark","Space"};
            combobox_CheckBit.SelectedIndex = 0;

            combobox_DataBit.ItemsSource = new String[]{ "5","6","7","8"};
            combobox_DataBit.SelectedIndex = 3;

            combobox_StopBit.ItemsSource = new String[]{ "1","1.5","2","0"};
            combobox_StopBit.SelectedIndex = 0;


        }

        private void InitSerialPort()
        {
            serialPort.PortName = combobox_SerialPortName.Text;
            serialPort.BaudRate = Convert.ToInt32(combobox_BaundRate.Text);
            serialPort.DataBits = Convert.ToInt32(combobox_DataBit.Text);

            switch(combobox_CheckBit.SelectedIndex)
            {
                case 0:
                    serialPort.Parity = Parity.None;
                    break;
                case 1:
                    serialPort.Parity = Parity.Odd;
                    break;
                case 2:
                    serialPort.Parity = Parity.Even;
                    break;
                case 3:
                    serialPort.Parity = Parity.Mark;
                    break;
                case 4:
                    serialPort.Parity = Parity.Space;
                    break;
            }

            switch(combobox_StopBit.SelectedIndex)
            {
                case 0:
                    serialPort.StopBits = StopBits.One;
                    break;
                case 1:
                    serialPort.StopBits = StopBits.OnePointFive;
                    break;
                case 2:
                    serialPort.StopBits = StopBits.Two;
                    break;
                case 3:
                    serialPort.StopBits = StopBits.None;
                    break;
            }
        }

        private void SendData()
        {
            try
            {
                if (checkBox_Entersend.IsChecked != null)
                {
                    if ((bool)checkBox_Entersend.IsChecked)
                    {
                        if(checkBox_Hexsend!=null)
                        {
                            if((bool)checkBox_Hexsend.IsChecked)
                            {
                                
                            }
                            else
                            {
                                serialPort.Write(textBox_send.Text + "\n");
                            }
                        }
                    }
                    else
                    {
                        if (checkBox_Hexsend != null)
                        {
                            if ((bool)checkBox_Hexsend.IsChecked)
                            {

                            }
                            else
                            {
                                serialPort.Write(textBox_send.Text);
                            }
                        }
                    }
                }
            }
            catch { }
        }

        private void btn_Send_Click(object sender, RoutedEventArgs e)
        {
            SendData();
        }

    }
}
